package city.spring.modules.medicine.service.impl;

import city.spring.modules.medicine.entity.MedicinereceiptEntity;
import city.spring.modules.medicine.entity.MedicinereceiptapprovalEntity;
import city.spring.modules.medicine.repository.MedicinereceiptapprovalRepository;
import city.spring.modules.medicine.service.MedicinereceiptService;
import city.spring.modules.medicine.service.MedicinereceiptapprovalService;
import city.spring.modules.system.entity.UserEntity;
import city.spring.modules.system.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.NonNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.security.Principal;
import java.util.Date;
import java.util.List;

/**
 * Service：药品领用审核
 *
 * @author 谢金婷
 * @date 2020-06-27 22:36:03
 */
@CacheConfig(cacheNames = {MedicinereceiptapprovalServiceImpl.CACHE_NAME})
@Transactional(rollbackFor = Throwable.class)
@Service
public class MedicinereceiptapprovalServiceImpl extends ServiceImpl<MedicinereceiptapprovalRepository, MedicinereceiptapprovalEntity> implements MedicinereceiptapprovalService {

    private static final Logger logger = LoggerFactory.getLogger(MedicinereceiptapprovalServiceImpl.class);

    private final MedicinereceiptService medicinereceiptService;
    private final UserService userService;

    public MedicinereceiptapprovalServiceImpl(MedicinereceiptService medicinereceiptService, UserService userService) {
        this.medicinereceiptService = medicinereceiptService;
        this.userService = userService;
    }

    @Override
    public void saveMedicinereceiptapproval(MedicinereceiptapprovalEntity entity, @NonNull Principal user) {


        //获取物资领用申请信息
        MedicinereceiptEntity medicinereceiptEntity = medicinereceiptService.getById(String.valueOf(entity.getReceiptId()));
//        logger.info("当前物资领用申请信息：{}", materialreceiptEntity);
//        System.out.println(materialreceiptEntity);
//        System.out.println(materialreceiptEntity.getReviewer());


        //获取当前登录用户信息
        UserEntity userEntity = userService.getUserByIdOrAccount(user.getName());
        logger.info("medicinereceiptEntity.getReviewer()", medicinereceiptEntity.getReviewer());
        logger.info("userEntity.getId()", userEntity.getId());
        //判断当前用户是否是该领用单领用人部门主管
        if (userEntity.getId().equals(medicinereceiptEntity.getReviewer())) {
            //设置领用申请单id
            entity.setReceiptId(medicinereceiptEntity.getId());
            /**
             * 修改领用申请单信息
             */
            //修改审核状态
            medicinereceiptEntity.setApprovalStatus(entity.getApprovalStatus());
            System.out.println("entity.getApprovalStatus()" + entity.getApprovalStatus());
            //审核结果
            medicinereceiptEntity.setSuggestion(entity.getSuggestion());
            //审核日期为当前系统日期
            medicinereceiptEntity.setReviewdate(new Date());

            // 调用更新方法
            medicinereceiptService.lambdaUpdate()
                    .set(MedicinereceiptEntity::getApprovalStatus, medicinereceiptEntity.getApprovalStatus())
                    .set(MedicinereceiptEntity::getSuggestion, medicinereceiptEntity.getSuggestion())
                    .set(MedicinereceiptEntity::getReviewdate, medicinereceiptEntity.getReviewdate())
                    .eq(MedicinereceiptEntity::getId, medicinereceiptEntity.getId())
                    .update();
        } else {
            throw new RuntimeException("您不能审核该领用申请单，该领用单由领用人部门主管审核。");
        }


        if (!save(entity)) {
            throw new RuntimeException("保存信息失败");
        }
    }

    @Override
    public void updateMedicinereceiptapproval(MedicinereceiptapprovalEntity entity) {
        // 使用 updateById(entity); 修改数据时，将会修改 entity 对象中所有非null数据，如果某个字段为null，将会忽略该字段的修改

        boolean update = updateById(entity);
        if (!update) {
            throw new RuntimeException("修改信息失败");
        }
    }

    @Override
    public void deleteMedicinereceiptapproval(String primaryKey) {
        removeById(primaryKey);
    }

    @Override
    public void deleteMedicinereceiptapproval(List<String> primaryKeys) {
        removeByIds(primaryKeys);
    }
}